#include "fit/rectangle_fitting.h"

RectangleFitting::RectangleFitting() {}

RectangleFitting::~RectangleFitting() {}

void RectangleFitting::Fit(std::vector<cv::Point> &points, RotatedRect &rect) {
  rect = cv::minAreaRect(Mat(points));
}

void RectangleFitting::Fit(const std::vector<Eigen::Vector2f> &in,
                           RotatedRect &rect, cv::Mat &mat) {
  std::vector<cv::Point2f> points;
  transform(in, points);
  rect = cv::minAreaRect(Mat(points));
  // std::cout << "最小外包旋转矩形的中心坐标：" << rect.center << std::endl;
  // std::cout << "最小外包旋转矩形的尺寸：" << rect.size << std::endl;
  // std::cout << "最小外包旋转矩形的旋转角度：" << rect.angle << std::endl;

  cv::boxPoints(rect, mat);
  // std::cout << "mat = " << mat << std::endl;
}

void RectangleFitting::transform(const std::vector<Eigen::Vector2f> &in,
                                 std::vector<cv::Point> &out) {
  out.clear();
  for (const Eigen::Vector2f &v : in) {
    cv::Point p;
    p.x = v.x();
    p.y = v.y();
    out.push_back(p);
  }
}

void RectangleFitting::transform(const std::vector<Eigen::Vector2f> &in,
                                 std::vector<cv::Point2f> &out) {
  out.clear();
  for (const Eigen::Vector2f &v : in) {
    cv::Point2f p;
    p.x = v.x();
    p.y = v.y();
    // std::cout << "transform = " << p.x << " " << p.y << std::endl;
    out.push_back(p);
  }
}